package com.zjdiepu.www.biz.icbc;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.icbc.YwOpenCardHzicbcSubMapper;
import com.zjdiepu.www.ibiz.icbc.IYwOpenCardHzicbcSubService;
import com.zjdiepu.www.model.icbc.dto.YwOpenCardHzicbcSub;
import com.zjdiepu.www.model.icbc.ext.YwOpenCardHzicbcSubExt;

/**
 * yw_开卡信息_杭州工行_子表Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年12月13日 18时05分<br/>
 * @since 1.0<br/>
 */
@Service
public class YwOpenCardHzicbcSubServiceImpl extends BaseService implements IYwOpenCardHzicbcSubService {
	public static final Logger logger = LoggerFactory.getLogger(YwOpenCardHzicbcSubServiceImpl.class);

	@Autowired
	private YwOpenCardHzicbcSubMapper ywOpenCardHzicbcSubMapper;
	
	@Override
	public void saveUpdateAndDelete(YwOpenCardHzicbcSub ywOpenCardHzicbcSub, OperateType operType) {
		switch (operType) {
		case add:
			ywOpenCardHzicbcSub.initInsertData(ywOpenCardHzicbcSub.getModifier(), ywOpenCardHzicbcSub.getModifierName());
			ywOpenCardHzicbcSubMapper.insert(ywOpenCardHzicbcSub);
			break;
		case edit:
			ywOpenCardHzicbcSub.initUpdateData(ywOpenCardHzicbcSub.getModifier(), ywOpenCardHzicbcSub.getModifierName());
			// 影响的记录数
			int updateCount = ywOpenCardHzicbcSubMapper.updateByPrimaryKeySelective(ywOpenCardHzicbcSub);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			ywOpenCardHzicbcSub.initDeleteData(ywOpenCardHzicbcSub.getModifier(), ywOpenCardHzicbcSub.getModifierName());
			// 影响的记录数
			int deleteCount = ywOpenCardHzicbcSubMapper.updateByPrimaryKeySelective(ywOpenCardHzicbcSub);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<YwOpenCardHzicbcSub> selectByExt(YwOpenCardHzicbcSubExt ywOpenCardHzicbcSubExt) {
		return ywOpenCardHzicbcSubMapper.selectByExt(ywOpenCardHzicbcSubExt);
	}
	
	@Override
	public int countByExt(YwOpenCardHzicbcSubExt ywOpenCardHzicbcSubExt){
		return ywOpenCardHzicbcSubMapper.countByExt(ywOpenCardHzicbcSubExt);
	}
	
	@Override
	public YwOpenCardHzicbcSub selectById(String id) {
		return ywOpenCardHzicbcSubMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			ywOpenCardHzicbcSubMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(YwOpenCardHzicbcSub ywOpenCardHzicbcSub) {
		return ywOpenCardHzicbcSubMapper.countRefined(ywOpenCardHzicbcSub);
	}

	@Override
	public YwOpenCardHzicbcSub selectRefined(YwOpenCardHzicbcSub ywOpenCardHzicbcSub) {
		return ywOpenCardHzicbcSubMapper.selectRefined(ywOpenCardHzicbcSub);
	}
	
	@Override
	public int deletedRefined(YwOpenCardHzicbcSub ywOpenCardHzicbcSub) {
		return ywOpenCardHzicbcSubMapper.deletedRefined(ywOpenCardHzicbcSub);
	}

	@Override
	public boolean repeat(YwOpenCardHzicbcSub ywOpenCardHzicbcSub) {
		int count = countRefined(ywOpenCardHzicbcSub);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(ywOpenCardHzicbcSub.getId())) {
			return true;
		}
		YwOpenCardHzicbcSub dbItem = selectRefined(ywOpenCardHzicbcSub);
		if(null == dbItem) {
			return false;
		}
		return !ywOpenCardHzicbcSub.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return ywOpenCardHzicbcSubMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<YwOpenCardHzicbcSub> ywOpenCardHzicbcSubs) {
		return ywOpenCardHzicbcSubMapper.insertBatch(ywOpenCardHzicbcSubs);
	}
}
